home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 5102 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.4 KB  |  82 lines

  1. Path: news.lpr.carel.fi!usenet
  2. From: Ari Lukumies <aril@cmt.lpr.mail.carel.fi>
  3. Newsgroups: comp.lang.c
  4. Subject: Re: typedefs for functions??
  5. Date: Thu, 08 Feb 1996 14:30:39 +0200
  6. Organization: Carelcomp Forest
  7. Message-ID: <3119ECEF.3B32@cmt.lpr.mail.carel.fi>
  8. References: <4fa7u0$516@stork.runit.sintef.no>
  9. NNTP-Posting-Host: renoir.cclahti.carel.fi
  10. Mime-Version: 1.0
  11. Content-Type: text/plain; charset=us-ascii
  12. Content-Transfer-Encoding: 7bit
  13. X-Mailer: Mozilla 2.0b6a (WinNT; I)
  14.  
  15. Per Henning Isaksen wrote:
  16. > The following code works as intended, but I would like to
  17. > change its 'looks'.
  18. >      1
  19. >      2  typedef void (*ActionFunction)(char* g);
  20. >      3
  21. >      4  void a(char* b, ActionFunction g) {
  22. >      5       ActionFunction p=g;
  23. >      6       (*p)(b);
  24. >      7       return;
  25. >      8  }
  26. >      9  void aa(char* g) { /* an ActionFunction */
  27. >     10       printf("..%s..\n", g);
  28. >     11       return;
  29. >     12  }
  30. >     13
  31. >     14
  32. >     15  void main() {
  33. >     16       a("adf", aa);
  34. >     17  }
  35. > Purpose: Inside a Motif GUI, I want to pass a function that
  36. > can perform some action, so I have the typdef in line 2 to
  37. > define ActionFunction.
  38. > I would like to change line 9 to:
  39. >    9  ActionFunction aa(char* g) {
  40. > But this causes a warning on line 16 (but it executes correctly)
  41. >    warning 604: Pointers are not assignment-compatible.
  42. >    warning 563: Argument #2 is not the correct type.
  43. > Of course, I could use a cast, but I wouldn't like it.
  44. > Anyone who can teach me the trick?
  45. > I want to state the function
  46. > type (via typdefs) both in the parameter list and for the declaration
  47. > of the function itself. (Why? I have lots of functions and I want want
  48. > to mark those that are ActionFunction as such - because some change must
  49. > be done to all functions of this type, but not to other function of
  50. > similar type (i.e. void (char*) in the example)). Obviously, I could
  51. > use a comment, but any error that can be caught by the compiler is
  52. > an error I can forget.
  53. > Per Henning
  54.  
  55. At one time I did something like this:
  56.  
  57.     typedef void    FUNCTYPE(char *g);
  58.     typedef    FUNCTYPE    *FUNCPTR;
  59.  
  60.     FUNCTYPE    aa
  61.     {
  62.         ...
  63.     }
  64.  
  65. and it worked just fine (FUNCTYPE aa became void aa(char *g)). But anyway, later I was told 
  66. that it's not standardized behaviour and thus may not compile on all platforms. Using this 
  67. example you'd use FUNCPTR (or equivalent) on your lines 4 and 5 above instead of 
  68. ActionFunction.
  69.  
  70. Later,
  71.  AriL
  72. -- 
  73. All my opinions are mine and mine alone.
  74.